/*
Problem Description
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
Output
For each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
8
A B E F G C D H
E B F G A C H D
7
A B D E C F G
D B E A F C G
Sample Output
7 4 2 8 9 5 6 3 1
E G F B H D C A
D E B F G C A
 */
package com.yuan.algorithms.training20150721;

import java.util.Scanner;

class BinTree {
	private Object data;// 数据域
	private BinTree lChild;// 左孩子
	private BinTree rChild;// 右孩子

	public BinTree(Object data) {
		this.data = data;
		lChild = rChild = null;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public BinTree getlChild() {
		return lChild;
	}

	public void setlChild(BinTree lChild) {
		this.lChild = lChild;
	}

	public BinTree getrChild() {
		return rChild;
	}

	public void setrChild(BinTree rChild) {
		this.rChild = rChild;
	}

}

public class 根据二叉树的前序和中序遍历求后序遍历 {
/*
 * 代码实现有问题（已经修复）。因为题目输入的是整数，而下面的代码将输入当做字符串处理，
 * 若是输入12则会被视为两个子节点
 */
	static boolean mark;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			String[] q = new String[n];
			String[] z = new String[n];
			mark = true;
			for (int i = 0; i < n; i++) {
				q[i] = sc.next();
			}
			for (int i = 0; i < n; i++) {
				z[i] = sc.next();
			}
			BinTree t = restoreTree(q,z);
			outputTreePostorder(t);
			System.out.println();
		}
	}

	/**
	 * 输出二叉树的后序遍历
	 * 
	 * @param BinTree 二叉树对象
	 */
	private static void outputTreePostorder(BinTree t) {
		if (t != null) {
			outputTreePostorder(t.getlChild());
			outputTreePostorder(t.getrChild());
			if (mark) {
				System.out.print(t.getData());
				mark = false;
			} else {
				System.out.print(" " + t.getData());
			}
		}
	}

	/**
	 * 根据二叉树的先序中序恢复二叉树
	 * 
	 * @param tree
	 *            保存二叉树先序、中序字符串序列的数组
	 * @return BinTree 二叉树对象
	 */
	private static BinTree restoreTree(String[] q, String[] z) {
		if (q==null) {
			return null;
		}
		String root = q[0];
		BinTree result = new BinTree(root);

		int index;
		for(index = 0; !z[index].equals(root); index++);//注意：此处是空循环，用于获取根节点在中序遍历的下标
		String[] newQl = subArray(q, 1, index+1);//截取前序左子树
		String[] newZl = subArray(z, 0, index);//中序左子树
		
		String[] newQr = subArray(q, index+1, q.length);//前序右子树
		String[] newZr = subArray(z, index+1, z.length);//中序右子树
		result.setlChild(restoreTree(newQl, newZl));//递归构建左子树
		result.setrChild(restoreTree(newQr, newZr));//递归构建右子树
		
		return result;//返回构建完成的树
	}

	/**
	 * 根据给定的开始结束位置截取字符串数组
	 * @param q
	 * @param i
	 * @param j
	 * @return
	 */
	private static String[] subArray(String[] q, int i, int j) {
		if (j-i<=0) {
			return null;
		}
		String[] t = new String[j-i];
		for (int k = i, y = 0; k < j; k++, y++) {
			t[y] = q[k];
		}
		return t;
	}

}
